package com.cwnu.result;


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @ProjectName: swagger-demo
 * @Titile: R
 * @Author: Lucky
 * @Description: 统一返回的结果实体
 */
//@ApiModel(value = "返回结果集的实体")
public class R {

    //@ApiModelProperty("code值 0：表示成功 非0：表示失败")
    private Integer code;    //返回的响应码
    //@ApiModelProperty("出错之后的错误的提示信息")
    private String msg;  //错误的提示信息
    //@ApiModelProperty("请求成功之后向客户端返回数据")
    //客户端请求服务端的返回数据
    private HashMap<String,Object> data=new HashMap<>();

    private R(){}


    /**
     * 这个表示的是请求成功
     * @return
     */
    public static R ok(){
        R r = new R();
        r.setCode(ResponceCode.SUCCESS.getCode());
        r.setMsg(ResponceCode.SUCCESS.getmsg());
        return r;
    }

    /**
     * 请求失败的封装
     * @return
     */
    public static R error(){
        R r = new R();
        r.setCode(ResponceCode.ERROR.getCode());
        r.setMsg(ResponceCode.ERROR.getmsg());
        return r;
    }


    /**
     * 向返回数据中添加键值对
     * @param key
     * @param val
     * @return
     */
    public R data(String key,Object val){
        this.data.put(key,val);
        return this;
    }

    /**
     * 向返回数据中添加键值对
     * @param map
     * @return
     */
    public R data(HashMap<String,Object> map){
        Set<Map.Entry<String, Object>> entries = map.entrySet();
        Iterator<Map.Entry<String, Object>> it = entries.iterator();
        while (it.hasNext()){
            Map.Entry<String, Object> next = it.next();
            String key = next.getKey();
            Object value = next.getValue();
            //放进原来的集合中
            this.data.put(key,value);
        }
        return this;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public HashMap<String, Object> getData() {
        return data;
    }

    public void setData(HashMap<String, Object> data) {
        this.data = data;
    }
}
